package com.lzy.webapi.facade;

import com.github.pagehelper.PageInfo;
import com.lzy.base.apis.SysUserAdminServiceApi;
import com.lzy.base.dtos.SysUserAdminDto;
import com.lzy.coupon.apis.MotorcycleServiceApi;
import com.lzy.coupon.apis.MotorcycleSpecServiceApi;
import com.lzy.coupon.dtos.MotorcycleDto;
import com.lzy.coupon.dtos.MotorcycleSpecDto;
import com.lzy.framework.core.datasource.multiple.DataSource;
import com.lzy.framework.core.datasource.multiple.DataSourceHolder;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.member.apis.MemberUserServiceApi;
import com.lzy.member.dtos.MemberUserDto;
import com.lzy.product.apis.*;
import com.lzy.product.common.util.UserUtil;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.product.dtos.ProjectDto;
import com.lzy.product.dtos.agent.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by WIN7 on 2018/4/13.
 */
@Component
public class AgentProtocolFacade {

    @Autowired
    private AgentProtocolServiceApi agentProtocolServiceApi;
    @Autowired
    private AgentProtocolImageTextServiceApi agentProtocolImageTextServiceApi;
    @Autowired
    private AgentProductProjectServiceApi agentProductProjectServiceApi;
    @Autowired
    private AgentProtocolYieldRatioServiceApi agentProtocolYieldRatioServiceApi;
    @Autowired
    private AgentProtocolProductServiceApi agentProtocolProductServiceApi;
    @Autowired
    private AgentProtocolSystemServiceApi agentProtocolSystemServiceApi;
    @Autowired
    private AgentProtocolCommissionServiceApi agentProtocolCommissionServiceApi;
    @Autowired
    private AgentPromoteServiceApi agentPromoteServiceApi;
    @Autowired
    private ProjectServiceApi projectServiceApi;
    @Autowired
    private MotorcycleSpecServiceApi motorcycleSpecServiceApi;
    @Autowired
    private MotorcycleServiceApi motorcycleServiceApi;
    @Autowired
    private AgentProductServiceApi agentProductServiceApi;
    @Autowired
    private ProductUserServiceApi productUserServiceApi;
    @Autowired
    private MemberUserServiceApi memberUserServiceApi;
    @Autowired
    private AgentMemberProtocolServiceApi agentMemberProtocolServiceApi;


    /**
     * 协议列表
     *
     * @param pager
     * @return
     */
    public PageInfo<AgentProtocolDto> agentProtocolList(Pager pager, Long userId) {
        //获取用户信息
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        ProductUserDto productUserDto = productUserServiceApi.findById(userId);
        if (null != productUserDto && null != productUserDto.getSystemId()) {
            AgentProtocolDto agentProtocolDto = new AgentProtocolDto();
            agentProtocolDto.setSystemId(productUserDto.getSystemId());
            agentProtocolDto.setStatus(Integer.valueOf("0"));
            PageInfo pageInfo = agentProtocolServiceApi.selectList(pager, agentProtocolDto);
            return pageInfo;
        }

        return null;
    }

    /**
     * 获取协议详情
     *
     * @param id
     * @return
     */
    public AgentProtocolInfoDto getAgentProtocolInfo(Long id, Long userId) {
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        //协议详情
        AgentProtocolDto agentProtocolDto = agentProtocolServiceApi.selectDetailById(id);
        //图文详情
        //List<AgentProtocolImageTextDto> agentProtocolImageTextDtoList = agentProtocolImageTextServiceApi.selectByProtocolId(id);
        //产品
        List<AgentProtocolProductDto> agentProtocolProductDtoList = agentProtocolProductServiceApi.selectByProtocolId(id);
        if (null != agentProtocolProductDtoList && !agentProtocolProductDtoList.isEmpty()) {
            for (AgentProtocolProductDto agentProtocolProductDto : agentProtocolProductDtoList) {
                AgentProductDto agentProductDto = agentProductServiceApi.queryAgentProductById(agentProtocolProductDto.getProductId());
                agentProtocolProductDto.setAgentProductDto(agentProductDto);
            }
        }
        //体系
        List<AgentProtocolSystemDto> agentProtocolSystemDtoList = agentProtocolSystemServiceApi.selectByProtocolId(id);
        //返现
        List<AgentProtocolYieldRatioDto> agentProtocolYieldRatioDtoList = agentProtocolYieldRatioServiceApi.selectByProtocolId(id);
        if (null != agentProtocolYieldRatioDtoList && !agentProtocolYieldRatioDtoList.isEmpty() && agentProtocolYieldRatioDtoList.size() >0) {
            for (AgentProtocolYieldRatioDto agentProtocolYieldRatioDto : agentProtocolYieldRatioDtoList) {
                BigDecimal subsidyAmount = agentProtocolDto.getPrice().multiply(agentProtocolYieldRatioDto.getCashbackRatio().divide(new BigDecimal("100")));
                agentProtocolYieldRatioDto.setSubsidyAmount(subsidyAmount);
                agentProtocolYieldRatioDto.setSubsidyDay(subsidyAmount.divide(new BigDecimal(agentProtocolYieldRatioDto.getCashbackTime()),2, BigDecimal.ROUND_HALF_DOWN));
            }
        }
        //分拥
        ProductUserDto productUserDto = productUserServiceApi.findById(userId);
        List<AgentProtocolCommissionDto> agentProtocolCommissionDtoList = new ArrayList<AgentProtocolCommissionDto>();
        if (null != productUserDto && null != productUserDto.getSystemId()) {
            agentProtocolCommissionDtoList = agentProtocolCommissionServiceApi.selectByProtocolIdAndSystemId(id, productUserDto.getSystemId(), null);
        }
        //代理商产品包含项
        List<AgentProductProjectDto> agentProductProjectDtoList = agentProductProjectServiceApi.selectByProtocolId(id,2);
        for (AgentProductProjectDto agentProductProjectDto : agentProductProjectDtoList) {
            ProjectDto projectDto = projectServiceApi.getById(agentProductProjectDto.getProjectId());
            if (null != agentProductProjectDto.getSpecId()) {
                DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
                MotorcycleSpecDto motorcycleSpecDto = motorcycleSpecServiceApi.selectBySpecId(agentProductProjectDto.getSpecId());
                MotorcycleDto motorcycleDto = motorcycleServiceApi.findById(motorcycleSpecDto.getMotorcycleId());
                AgentProductMotorcycleDto agentProductMotorcycleDto = new AgentProductMotorcycleDto();
                agentProductMotorcycleDto.setMotorcycleId(motorcycleSpecDto.getId());
                agentProductMotorcycleDto.setMotorcycleName(motorcycleDto.getName() + "(" + motorcycleSpecDto.getPrice() + "元)");
                agentProductProjectDto.setAgentProductMotorcycleDto(agentProductMotorcycleDto);
                DataSourceHolder.clearDataSource();
            }
            agentProductProjectDto.setProjectDto(projectDto);
        }
        //返回数据
        AgentProtocolInfoDto agentProtocolInfoDto = new AgentProtocolInfoDto();
        agentProtocolInfoDto.setAgentProtocolDto(agentProtocolDto);
        agentProtocolInfoDto.setAgentProtocolProductDtoList(agentProtocolProductDtoList);
        agentProtocolInfoDto.setAgentProtocolSystemDtoList(agentProtocolSystemDtoList);
        agentProtocolInfoDto.setAgentProtocolYieldRatioDtoList(agentProtocolYieldRatioDtoList);
        agentProtocolInfoDto.setAgentProtocolCommissionDtoList(agentProtocolCommissionDtoList);
        agentProtocolInfoDto.setAgentProductProjectDtoList(agentProductProjectDtoList);
        return agentProtocolInfoDto;
    }

    /**
     * 协议列表
     *
     * @return
     */
    public List<AgentProtocolDto> queryProtocolList() {
        List<AgentProtocolDto> agentProtocolDtoList = agentProtocolServiceApi.queryList();
        return agentProtocolDtoList;
    }

    public Map<String,Object> getProduct(Long id, Integer type){
        Map<String,Object> map = new HashMap<>();
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_COMMON);
        MemberUserDto memberUserDto = memberUserServiceApi.findById(id);
        DataSourceHolder.setDataSource(DataSource.DATASOURCE_PRODUCT);
        ProductUserDto productUserDto = productUserServiceApi.queryByPhone(memberUserDto.getAccount());
        AgentMemberProtocolDto agentMemberProtocolDto = new AgentMemberProtocolDto();
        agentMemberProtocolDto.setMemberId(productUserDto.getId());
        map.put("state",productUserDto.getState());
        List<AgentMemberProtocolDto> agentMemberProtocolDtos = agentMemberProtocolServiceApi.query(agentMemberProtocolDto);
        if(null != agentMemberProtocolDtos && agentMemberProtocolDtos.size() > 0){
            AgentMemberProtocolDto agentMemberProtocolDto1 = agentMemberProtocolDtos.get(0);
            AgentProtocolDto agentProtocolDto = agentProtocolServiceApi.selectDetailById(agentMemberProtocolDto1.getProtocolId());
            //判断该协议是否为汽摩券销售权力
            if(null != agentProtocolDto.getType() && !"".equals(agentProtocolDto.getType())){
                if(type == 1 && agentProtocolDto.getType() == 1){
                    map.put("type",1);
                }
                if(type == 2){
                    List<AgentProductDto> agentProductDtoList = new ArrayList<>();
                    List<AgentProtocolProductDto> agentProtocolProductDtoList = agentProtocolProductServiceApi.selectByProtocolId(agentMemberProtocolDto1.getProtocolId());
                    for(AgentProtocolProductDto agentProtocolProductDto : agentProtocolProductDtoList){
                        agentProductDtoList.add(agentProductServiceApi.queryAgentProductById(agentProtocolProductDto.getProductId()));
                    }
                    map.put("type",2);
                    map.put("list",agentProductDtoList);
                }
            }
        }
        return map;
    }
}
